#!/bin/sh
# Script to install gentoo i386 release into a jail
# Author: Kris Moore <kris@pcbsd.org>

SELF="gentoo-stage3-i486"
SELFDIR="${JDIR}/.${SELF}"
INDEXFILE="latest-stage3-i486.txt"
ERRORFILE="/tmp/.${SELF}.error"

get_gentoo_mirrors()
{
    local SUBDIR="releases/x86/autobuilds"

    cat<<__EOF__
    http://ftp.roedu.net/pub/mirrors/gentoo.org/$SUBDIR/
    http://distro.ibiblio.org/pub/linux/distributions/gentoo/$SUBDIR/
    http://gentoo.mirrors.pair.com/$SUBDIR/
    http://gentoo.chem.wisc.edu/gentoo/$SUBDIR/
    http://adelie.polymtl.ca/$SUBDIR/
    http://mirrors.tds.net/gentoo/$SUBDIR/
    ftp://ftp.mirrorservice.org/sites/www.ibiblio.org/gentoo/$SUBDIR/
    http://ftp.belnet.be/mirror/rsync.gentoo.org/gentoo/$SUBDIR/
    http://ftp.easynet.nl/mirror/gentoo/$SUBDIR/
    http://ftp.snt.utwente.nl/pub/os/linux/gentoo/$SUBDIR/
    http://trumpetti.atm.tut.fi/gentoo/$SUBDIR/
    http://sunsite.cnlab-switch.ch/ftp/mirror/gentoo/$SUBDIR/
    http://linux.rz.ruhr-uni-bochum.de/download/gentoo-mirror/$SUBDIR/
    http://ftp.uni-erlangen.de/pub/mirrors/gentoo/$SUBDIR/
    http://darkstar.ist.utl.pt/gentoo/$SUBDIR/
    http://gentoo.inode.at/$SUBDIR/
    http://ftp.rhnet.is/pub/gentoo/$SUBDIR/
    http://gentoo.mirror.sdv.fr/$SUBDIR/
    http://gentoo.gg3.net/$SUBDIR/
    http://gentoo.kems.net/$SUBDIR/
    ftp://ftp.gtlib.gatech.edu/pub/gentoo/$SUBDIR/
    ftp://mirror.iawnet.sandia.gov/pub/gentoo/$SUBDIR/
    ftp://ftp.ussg.iu.edu/pub/linux/gentoo/$SUBDIR/
    ftp://ftp.ucsb.edu/pub/mirrors/linux/gentoo/$SUBDIR/
    ftp://gentoo.mirrors.pair.com/$SUBDIR/
    ftp://mirrors.tds.net/gentoo/$SUBDIR/
    ftp://ftp.belnet.be/mirror/rsync.gentoo.org/gentoo/$SUBDIR/
    ftp://ftp.easynet.nl/mirror/gentoo/$SUBDIR/
    ftp://ftp.snt.utwente.nl/pub/os/linux/gentoo/$SUBDIR/
    ftp://trumpetti.atm.tut.fi/gentoo/$SUBDIR/
    ftp://sunsite.cnlab-switch.ch/mirror/gentoo/$SUBDIR/
    ftp://ftp.join.uni-muenster.de/pub/linux/distributions/gentoo/$SUBDIR/
    ftp://ftp6.uni-muenster.de/pub/linux/distributions/gentoo/$SUBDIR/
    ftp://ftp.tu-clausthal.de/pub/linux/gentoo/$SUBDIR/
    ftp://sunsite.informatik.rwth-aachen.de/pub/Linux/gentoo/$SUBDIR/
    ftp://linux.rz.ruhr-uni-bochum.de/gentoo-mirror/$SUBDIR/
    ftp://ftp.uni-erlangen.de/pub/mirrors/gentoo/$SUBDIR/
    ftp://darkstar.ist.utl.pt/pub/gentoo/$SUBDIR/
    ftp://gentoo.inode.at/source/$SUBDIR/
    ftp://ftp.rhnet.is/pub/gentoo/$SUBDIR/
    ftp://files.gentoo.gr/$SUBDIR/
    ftp://ftp.ecc.u-tokyo.ac.jp/GENTOO/$SUBDIR/
    ftp://gentoo.kems.net/pub/mirrors/gentoo/$SUBDIR/
__EOF__
}

get_gentoo_mirror_list()
{
   local file="${1}"
   local gentoo_mirrors="$(get_gentoo_mirrors)"

   local mirrors=
   for m in ${gentoo_mirrors}
   do
       mirrors="${mirrors} ${m}/${1}"
   done

   echo "${mirrors}"
}

get_linux_file()
{
   local _rf="${1}"
   local _lf="${2}"

   local aDir="$(dirname $_lf)"
   local aFile="$(basename $_lf)"

   local astatfile="${HOME}/.${SELF}-aria-stat-i386"
   if [ -e "${astatfile}" ] ; then
     local astat="--server-stat-of=${astatfile}
        --server-stat-if=${astatfile}
        --uri-selector=adaptive
        --server-stat-timeout=864000"
   else
     local astat=" --server-stat-of=${astatfile} --uri-selector=adaptive "
   fi
   touch $astatfile

   local mirrors="$(get_gentoo_mirror_list ${1})"

   aria2c -k 5M \
      ${astat} \
      --check-certificate=false \
      --file-allocation=none \
      -d ${aDir} \
      -o ${aFile} \
      ${mirrors}

   return $?
}

get_dlfile_name()
{
    local archivefile="`cat ${SELFDIR}/${INDEXFILE} | grep 'stage3'`"
    local dlfile="`basename ${archivefile}`"

    echo "${dlfile}"
}

get_distfiles()
{
    rm -f "${ERRORFILE}"

    if [ ! -d "${SELFDIR}" ] ; then
      mkdir -p "${SELFDIR}"
    fi

    if [ ! -f "${SELFDIR}/${INDEXFILE}" ] ; then
      echo "Trying to fetch index: ${INDEXFILE}"
      get_linux_file "${INDEXFILE}" "${SELFDIR}/${INDEXFILE}"
    fi

    local archivefile="`cat ${SELFDIR}/${INDEXFILE} | grep 'stage3'`"
    local dlfile="`basename ${archivefile}`"

    if [ -z "${archivefile}" ] ; then
      echo "Failed to locate current archive"
      touch "${ERRORFILE}"      
      exit 1
    fi

    if [ ! -f "${SELFDIR}/${dlfile}" ] ; then
      echo "Fetching archive: ${archivefile}"
      get_linux_file "${archivefile}" "${SELFDIR}/${dlfile}"
    fi

    if [ ! -e "${SELFDIR}/${dlfile}" ] ; then
      echo "Failed to download.."
      touch "${ERRORFILE}"      
      exit 1
    fi
}

template_install()
{
    rm -f "${ERRORFILE}"

    local TDIR="${1}"
    if [ -z "${TDIR}" ] ; then 
      echo "TDIR is not defined"
      exit 1
    fi

    get_distfiles "$@"

    local dlfile="$(get_dlfile_name)"
    if [ -z "${dlfile}" ] ; then 
      touch "${ERRORFILE}"      
      exit 1
    fi

    echo "Extracting Gentoo base...Please wait..."
    tar --exclude=./dev -xpy --exclude=./proc --exclude=./lib/udev \
        --exclude=./lib/udev-state -C ${TDIR} -f "${SELFDIR}/${dlfile}"
    err=$?

    mkdir ${TDIR}/dev >/dev/null 2>/dev/null
    mkdir ${TDIR}/dev/fd >/dev/null 2>/dev/null
    mkdir ${TDIR}/proc >/dev/null 2>/dev/null
    mkdir ${TDIR}/sys >/dev/null 2>/dev/null

    umount -f ${TDIR}/dev/fd >/dev/null 2>/dev/null
    umount -f ${TDIR}/dev >/dev/null 2>/dev/null
    umount -f ${TDIR}/sys >/dev/null 2>/dev/null
    umount -f ${TDIR}/proc >/dev/null 2>/dev/null

    # Get rid of umount
    mv ${TDIR}/bin/umount ${TDIR}/bin/umount.prev
    ln -s /bin/true ${TDIR}/bin/umount

    return $err
}

jail_configure()
{
    rm -f "${ERRORFILE}"

    local JMETADIR="${1}"
    if [ -z "${JMETADIR}" ] ; then 
      echo "JMETADIR is not defined"
      touch "${ERRORFILE}"      
      exit 1
    fi

    # Setup some custom start / stop stuff
    echo "/sbin/rc default" > ${JMETADIR}/jail-start
    echo "/sbin/rc shutdown" > ${JMETADIR}/jail-stop
}

error()
{
    if [ -f "${ERRORFILE}" ] ; then
        rm "${ERRORFILE}"
        return 1
    fi

    return 0
}

main()
{
    case "${1}" in
      get_distfiles) shift; get_distfiles "$@" ;; 
      template_install) shift; template_install "$@" ;;
      jail_configure) shift; jail_configure "$@" ;;
      error) shift; error "$@" ;;
    esac

    if [ "$?" != "0" ] ; then
      touch "${ERRORFILE}"
    fi
}


main "$@"
